package com.hsxc.cdz.controller.appController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hsxc.cdz.base.bean.Station;
import com.hsxc.cdz.mgmt.station.StationManager;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by 陈钊 on 2016/10/13.
 *
 * @description
 */
@Controller("appStation")
@RequestMapping("/app")
public class StationController {
    @Autowired
    private StationManager stationManager;

    private static Logger logger = LoggerFactory.getLogger(StationController.class);

    @RequestMapping(value = "/getStationByName", method = RequestMethod.POST)
    @ResponseBody
    public Object getStationByName(@RequestBody String params) {
        JSONObject result = new JSONObject();
        String name = null;
        double latitude = 0;
        double longitude = 0;
        JSONObject param = JSONObject.fromObject(params);
        result.put("success", true);
        if (param.has("name"))
            name = param.getString("name");
        if (param.has("latitude"))
            latitude = param.getDouble("latitude");
        if (param.has("longitude"))
            longitude = param.getDouble("longitude");
        List<Map<String, Object>> stations = stationManager.getStationByStationName(name);
        if (stations == null || stations.size() == 0) {
            result.put("errcode", 1);
            result.put("message", "没有结果");
        }
        List<Map<String, Object>> stations_dis=new ArrayList<Map<String,Object>>();
        result.put("errcode", 0);
        if (stations != null) {
            for (int i = 0; i < stations.size(); i++) {
                Map<String, Object> resultMap = stations.get(i);
                long count = (long) resultMap.get("count");
                double lon = (double) resultMap.get("longitude") / count;
                double lat = (double) resultMap.get("latitude") / count;
                double distance = this.distance(longitude, latitude, lon, lat);
                resultMap.put("distance", distance);
                resultMap.remove("longitude");
                resultMap.remove("latitude");
                resultMap.put("longitude", lon);
                resultMap.put("latitude", lat);
                stations_dis.add(resultMap);
            }
        }
        ObjectMapper mapper = new ObjectMapper();
        String re = null;
        try {
            re = mapper.writeValueAsString(stations_dis);
            logger.info(re);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            result.put("errcode", 2);
            result.put("message", "您没有预约记录");
            logger.info("json 序列化异常");
            return result;
        }
        result.put("data", stations_dis);
        return result;
    }

    /**
     * 计算地球上任意两点(经纬度)距离
     *
     * @param long1 第一点经度
     * @param lat1  第一点纬度
     * @param long2 第二点经度
     * @param lat2  第二点纬度
     * @return 返回距离 单位：米
     */
    public double distance(double long1, double lat1, double long2, double lat2) {
        double a, b, R;
        R = 6378137; // 地球半径
        lat1 = lat1 * Math.PI / 180.0;
        lat2 = lat2 * Math.PI / 180.0;
        a = lat1 - lat2;
        b = (long1 - long2) * Math.PI / 180.0;
        double d;
        double sa2, sb2;
        sa2 = Math.sin(a / 2.0);
        sb2 = Math.sin(b / 2.0);
        d = 2 * R * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1) * Math.cos(lat2) * sb2 * sb2));
        return d;
    }
}
